/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

//import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import common.Configuration;

import models.QuestionGroupBelongingDTO;

/**
 *
 * @author User
 */
//public class QuestionGroupBelongingServiceImpl implements QuestionGroupBelongingService
public class QuestionGroupBelongingServiceImpl implements IEntity
{
    private Statement stmt=null;
    private ResultSet rst =null;
    private QuestionGroupBelongingDTO questionGroupBelonging;
    private List<QuestionGroupBelongingDTO> questionGroupBelongingList;
    
    public QuestionGroupBelongingDTO GetById(int id) {
        try {
        	
            stmt =  Configuration.GetStatement();
            rst = stmt.executeQuery("select * from questiongroupbelonging where idQuestionGroupBelonging =" + id); //select resultset
            questionGroupBelonging.setIdQuestionGroupBelonging(id);
            questionGroupBelonging.setQuestionGroupId(rst.getInt("QuestionGroupId"));
            questionGroupBelonging.setSurveyId(rst.getInt("SurveyId"));
            Configuration.CloseConnection();
       
        }catch (SQLException ex) {
            Logger.getLogger(QuestionGroupBelongingServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        return questionGroupBelonging;
    }

    public List<QuestionGroupBelongingDTO> GetAll() {
         
    	try
         {
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from questiongroupbelonging "); //select resultset
            while(rst.next()){
            questionGroupBelonging.setIdQuestionGroupBelonging(rst.getInt("idQuestionGroupBelonging"));
            questionGroupBelonging.setQuestionGroupId(rst.getInt("QuestionGroupId"));
            questionGroupBelonging.setSurveyId(rst.getInt("SurveyId"));
            questionGroupBelongingList.add(questionGroupBelonging);
            }
            Configuration.CloseConnection();

    }
        catch (SQLException ex) {
            Logger.getLogger(QuestionGroupBelongingServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         return questionGroupBelongingList;
    }
    
    public void Add(Object obj) {
    	
        QuestionGroupBelongingDTO objCast =(QuestionGroupBelongingDTO)obj;
        try {
			
        	stmt =  Configuration.GetStatement();
        	stmt.execute("INSERT INTO dbsurvey.questiongroupbelonging (`idQuestionGroupBelonging`, `QuestionGroupId`, `SurveyId`) VALUES ("+objCast.getIdQuestionGroupBelonging()+","+objCast.getQuestionGroupId()+", "+objCast.getSurveyId()+")");
        	Configuration.CloseConnection();
        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    public void Update(Object obj) {
        QuestionGroupBelongingDTO objCast =(QuestionGroupBelongingDTO)obj;

        try {
			
        	stmt = Configuration.GetStatement();
        	stmt.execute("update dbsurvey.questiongroupbelonging set  QuestionGroupId="+objCast.getQuestionGroupId()+"and SurveyId="+objCast.getSurveyId()+" where idQuestionGroupBelonging="+objCast.getIdQuestionGroupBelonging());
        	Configuration.CloseConnection();
        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    public void Remove(int id)  {
     
    	try {
			
    		stmt = Configuration.GetStatement();
    		stmt.execute("delete from questiongroupbelonging where idQuestionGroupBelonging =" + id);
    		Configuration.CloseConnection();
    		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }


}
